<template>
  <div>
    <a-modal class="form"
             v-model="dialogVisible"
             title="权限设置"
             width="712px"
             :dialog-style="{ top: '15vh' }"
             ok-text="确认"
             cancel-text="取消"
             @ok="handleOk()"
             @cancel="cancelOK()">
      <div class="box">
        <div class="left"
             v-bar>
          <div style="padding:16px">
            <a-input-search style="margin-bottom: 16px"
                            placeholder="Search"
                            @change="onChange" />
            <a-tree v-model="checkedKeys"
                    checkable
                    :expanded-keys="expandedKeys"
                    :auto-expand-parent="autoExpandParent"
                    :selected-keys="selectedKeys"
                    :tree-data="treeData"
                    @expand="onExpand"
                    @select="onSelect" />
          </div>
        </div>
        <div class="right"
             v-bar>
          <div style="padding:16px">
            <p>已选权限：2个</p>
            <div>
              <li>功能操作
                <img src=""
                     alt="">
              </li>
              <li>功能操作
                <img src=""
                     alt="">
              </li>
              <li>功能操作
                <img src=""
                     alt="">
              </li>
              <li>功能操作
                <img src=""
                     alt="">
              </li>
              <li>功能操作
                <img src=""
                     alt="">
              </li>
              <li>功能操作
                <img src=""
                     alt="">
              </li>
              <li>功能操作
                <img src=""
                     alt="">
              </li>
              <li>功能操作
                <img src=""
                     alt="">
              </li>
              <li>功能操作
                <img src=""
                     alt="">
              </li>
              <li>功能操作
                <img src=""
                     alt="">
              </li>
              <li>功能操作
                <img src=""
                     alt="">
              </li>
              <li>功能操作
                <img src=""
                     alt="">
              </li>
            </div>
          </div>
        </div>
      </div>

    </a-modal>
  </div>
</template>
<script>
const treeData = [
  {
    title: '0-0',
    key: '0-0',
    children: [
      {
        title: '0-0-0',
        key: '0-0-0',
        children: [
          { title: '0-0-0-0', key: '0-0-0-0' },
          { title: '0-0-0-1', key: '0-0-0-1' },
          { title: '0-0-0-2', key: '0-0-0-2' },
        ],
      },
      {
        title: '0-0-1',
        key: '0-0-1',
        children: [
          { title: '0-0-1-0', key: '0-0-1-0' },
          { title: '0-0-1-1', key: '0-0-1-1' },
          { title: '0-0-1-2', key: '0-0-1-2' },
        ],
      },
      {
        title: '0-0-2',
        key: '0-0-2',
      },
    ],
  },
  {
    title: '0-1',
    key: '0-1',
    children: [
      {
        title: '0-0-0',
        key: '0-0-0',
        children: [
          { title: '0-0-0-0', key: '0-0-0-0' },
          { title: '0-0-0-1', key: '0-0-0-1' },
          { title: '0-0-0-2', key: '0-0-0-2' },
        ],
      },
      {
        title: '0-0-1',
        key: '0-0-1',
        children: [
          { title: '0-0-1-0', key: '0-0-1-0' },
          { title: '0-0-1-1', key: '0-0-1-1' },
          { title: '0-0-1-2', key: '0-0-1-2' },
        ],
      },
      {
        title: '0-0-2',
        key: '0-0-2',
      },
    ],
  },
  {
    title: '0-2',
    key: '0-2',
    children: [
      { title: '0-1-0-0', key: '0-1-0-0' },
      { title: '0-1-0-1', key: '0-1-0-1' },
      { title: '0-1-0-2', key: '0-1-0-2' },
    ],
  },
  {
    title: '0-3',
    key: '0-3',
  },
];
import '@/assets/css/form.less'
const x = 3;
const y = 2;
const z = 1;
const gData = [];

const generateData = (_level, _preKey, _tns) => {
  const preKey = _preKey || '0';
  const tns = _tns || gData;

  const children = [];
  for (let i = 0; i < x; i++) {
    const key = `${preKey}-${i}`;
    tns.push({ title: key, key, scopedSlots: { title: 'title' } });
    if (i < y) {
      children.push(key);
    }
  }
  if (_level < 0) {
    return tns;
  }
  const level = _level - 1;
  children.forEach((key, index) => {
    tns[index].children = [];
    return generateData(level, key, tns[index].children);
  });
};
generateData(z);

const dataList = [];
const generateList = data => {
  for (let i = 0; i < data.length; i++) {
    const node = data[i];
    const key = node.key;
    dataList.push({ key, title: key });
    if (node.children) {
      generateList(node.children);
    }
  }
};
generateList(gData);

const getParentKey = (key, tree) => {
  let parentKey;
  for (let i = 0; i < tree.length; i++) {
    const node = tree[i];
    if (node.children) {
      if (node.children.some(item => item.key === key)) {
        parentKey = node.key;
      } else if (getParentKey(key, node.children)) {
        parentKey = getParentKey(key, node.children);
      }
    }
  }
  return parentKey;
};
export default {
  name: 'UserEditForm',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
  },
  data () {
    return {
      expandedKeys: ['0-0-0', '0-0-1'],
      autoExpandParent: true,
      checkedKeys: ['0-0-0'],
      selectedKeys: [],
      treeData,
      form: {
      },
      dialogVisible: false
    }
  },

  watch: {
    checkedKeys (val) {
      console.log('onCheck', val);
    },
    visible (newVal) {
      this.dialogVisible = newVal;

    }
  },
  mounted () {
    console.log(123123)
  },
  created () {
  },
  methods: {
    onExpand (expandedKeys) {
      console.log('onExpand', expandedKeys);
      // if not set autoExpandParent to false, if children expanded, parent can not collapse.
      // or, you can remove all expanded children keys.
      this.expandedKeys = expandedKeys;
      this.autoExpandParent = false;
    },
    onCheck (checkedKeys) {
      console.log('onCheck', checkedKeys);
      this.checkedKeys = checkedKeys;
    },
    onChange (e) {
      const value = e.target.value;
      const expandedKeys = dataList
        .map(item => {
          if (item.title.indexOf(value) > -1) {
            return getParentKey(item.key, gData);
          }
          return null;
        })
        .filter((item, i, self) => item && self.indexOf(item) === i);
      Object.assign(this, {
        expandedKeys,
        searchValue: value,
        autoExpandParent: true,
      });
    },
    onSelect (selectedKeys, info) {
      console.log('onSelect', info);
      this.selectedKeys = selectedKeys;
    },
    //获取列表
    tenantAddress () {
      this.clickStatus = true
    },
    //修改编辑
    getList () {
      this.clickStatus = false
    },

    //确定新增
    handleOk () {

    },
    //取消新增 如果form表单没值就是新建取消  有值是修改取消
    cancelOK () {
      this.$emit('cancel')
      // this.concel()
    },

  },
}
</script>
<style lang="less" scoped>
.box {
  overflow: hidden;
  > div {
    float: left;
  }
  .left {
    width: 332px;
    height: 393px;
    border: 1px solid @newBorderS;
    border-radius: 2px 0px 0px 2px;
    background: #ffffff;
  }
  .right {
    width: 332px;
    height: 393px;
    background-color: #fafafa;
    border: 1px solid @newBorderS;
    border-left: none;
    p {
      height: 32px;
      line-height: 32px;
      font-size: 14px;
      color: @newColorGreyFontM;
      margin-bottom: 16px;
    }
    > div {
      width: 300px;
      li {
        height: 32px;
        font-size: 12px;
        line-height: 30px;
        border: 1px dashed @newBorderS;
        background: #fff;
        border-radius: 2px;
        margin-bottom: 8px;
        cursor: pointer;
        padding-left: 24px;
        position: relative;
        img {
          position: absolute;
          right: 0;
          top: 0;
          width: 14px;
          height: 14px;
          background: pink;
          display: none;
        }
      }
      li:hover {
        color: @activeRed;
        border: 1px dashed @activeRed;
      }
      li:hover img {
        display: block;
      }
    }
  }
}
/deep/ .ant-modal-header {
  border-bottom: 1px solid @newBorderS;
}
/deep/ .ant-modal-footer {
  padding: 16px 24px;
}
/deep/ .ant-btn {
  width: 72px;
  height: 32px;
  border-radius: 2px;
  font-size: 12px;
}
</style>